﻿using CMS.Utility;
using DBMode;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModels
{
    public class AddUserViewModel :  BindableBase,IDialogAware
    {
        private readonly IMethods<Member> memberMethods;
        public AddUserViewModel(IMethods<Member> memberMethods)
        {
            this.memberMethods = memberMethods;
        }
        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var a = parameters.GetValue<object>("addUser");
        }

        private Member member=new Member();


        public Member Member
        {
            get { return member; }
            set { member = value;RaisePropertyChanged(); }
        }

        public DelegateCommand RegisterCommand { get { return new DelegateCommand(Register); } }

        private void Register()
        {
            if (Member.Name == null || Member.Password == null || Member.Role == null) return;
            Member.Password = Member.Password.EnMD5();
            int a = memberMethods.Add(Member);
            if (a == 0) MessageBox.Show("添加失败"); else {
                MessageBox.Show("添加成功");
                //IDialogParameters dialogParameters = new DialogParameters();
                //dialogParameters.Add("","")
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
        }
    }
}
